
var mData

function HideDOTATooltip(){
    $.DispatchEvent("DOTAHideTextTooltip");
    // $.DispatchEvent("DOTAHideTitleTextTooltip");
}

function ShowDOTATooltip(targetId){
    switch(targetId){
        case "HeroAvatar":
            $.DispatchEvent("DOTAShowTextTooltip", $("#" + targetId), "#"+mData.heroName);
            break;
        case "BotAvatar":
            $.DispatchEvent("DOTAShowTextTooltip", $("#" + targetId), "#DOTA_Lobby_Prefs_Bots");
            break;
    }
}


(function(Global /*, context */) {


    function SetupTooltips(panel) {

        panel.SetPanelEvent("onmouseover", function () {
            // $.Msg(abilityImgELE)
            $.DispatchEvent('DOTAShowAbilityTooltip',panel,panel.abilityname );
        })
        panel.SetPanelEvent("onmouseout",  function (){
            $.DispatchEvent('DOTAHideAbilityTooltip');
        })
    }

    function SetupRankTextTips(panel,grade) {
        panel.SetPanelEvent("onmouseover", function () {
            // $.Msg(abilityImgELE)
            $.DispatchEvent('DOTAShowTextTooltip',panel,'DOTARankTierName'+grade );
        })
        panel.SetPanelEvent("onmouseout",  function (){
            $.DispatchEvent('DOTAHideTextTooltip');
        })
    }

    function Toggle(isOpen) {
        if(isOpen==true){
            Game.EmitSound("Shop.PanelUp")
            $("#DetailInfo").style.visibility="visible"
            $("#RankContainer").style.marginRight="609px"
            $("#BotAvatar").style.visibility="collapse";
            $("#Avatar").style.visibility="collapse";
            $("#HeroAvatar").style.visibility="visible";
        }
        else {
            Game.EmitSound("Shop.PanelDown")
            $("#DetailInfo").style.visibility="collapse"
            $("#RankContainer").style.marginRight="60px"
            if(mData&&mData.isBot==true){
                $("#BotAvatar").style.visibility="visible";
                $("#Avatar").style.visibility="collapse";
            }
            else {
                $("#Avatar").style.visibility="visible";
                $("#BotAvatar").style.visibility="collapse";
            }
            $("#HeroAvatar").style.visibility="collapse";
        }
    }



    function SetItemData(data)
    {
        // $.Msg(data)
        mData=data
        if(Number(data.playerId)===Players.GetLocalPlayer()){
            $.GetContextPanel().SetHasClass("LocalPlayer",true);
        }
        $( "#UserName" ).text=data.playerName;
        $("#Avatar").steamid=data.steamId;
        $("#RankLogo").SetImage("s2r://panorama/images/rank_tier_icons/rank"+data.grade+"_psd.vtex");
        $("#KillInfo").text=data.kills+"/"+data.deaths;
        if(data.color){
            $("#UserNameContainer").style.backgroundColor="gradient( linear, 0% 100%, 0% 0%, from("+data.color+"), to(#979797))";
        }

        $("#HpBarText").text = data.life + "%";
        $("#HpBar").style.width = data.life + "%";
        if(data.isBot==true){
            $("#BotAvatar").style.visibility="visible";
            $("#Avatar").style.visibility="collapse";
        }
        else {
            $("#Avatar").style.visibility="visible";
            $("#BotAvatar").style.visibility="collapse";
        }
        $("#HeroAvatar").heroname = data.heroName;

        SetupRankTextTips($("#RankLogo"),data.grade)
        for(var i=1;i<= Global.GameConfig.abilitiesSlotCount;i++){
            var abilityImgELE=  $("#AbilitiesInfo").FindChildTraverse("AbilityImg"+i)
            // $.Msg(abilityImgELE)
            if(abilityImgELE===null){
                var id="AbilityImg"+i
                $("#AbilitiesInfo").BCreateChildren("<DOTAAbilityImage id='"+id+"' />");
                abilityImgELE=$("#AbilitiesInfo").FindChildTraverse("AbilityImg"+i)
            }
            abilityImgELE.abilityname=data.abilityNames[i]
            SetupTooltips(abilityImgELE)
        }
    }

    $.GetContextPanel().SetItemData = SetItemData;
    $.GetContextPanel().Toggle = Toggle;

})(GameUI.CustomUIConfig(), this);